<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据平台</el-breadcrumb-item>
      <el-breadcrumb-item>系统列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @click="getSystemInfo"
          >
            <el-button slot="append" icon="el-icon-search" @click="getSystemInfo"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true ">添加</el-button>
        </el-col>
      </el-row>
      <el-table :data="systemInfo" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="代码" prop="code"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="IP" prop="ip"></el-table-column>
        <el-table-column label="数据源类型" prop="db_type"></el-table-column>
        <el-table-column label="连接字符串" prop="connection"></el-table-column>
        <el-table-column label="文件夹" prop="folder"></el-table-column>
        <el-table-column label="读写 标识" prop="read_write_flag" :formatter="flagFormat" width="60px"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              content="修改"
              @click="showUpdateDialog(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              content="删除"
              @click="deleteSystemInfo(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 创建新用户对话框 -->
    <el-dialog title="添加" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <!-- 用户名 -->
        <el-form-item label="代码" prop="code">
          <el-input v-model="addForm.code"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="IP" prop="ip">
          <el-input v-model="addForm.ip"></el-input>
        </el-form-item>
        <el-form-item label="数据源类型" prop="db_type">
          <el-input v-model="addForm.db_type"></el-input>
        </el-form-item>
        <el-form-item label="连接字符串" prop="connection">
          <el-input v-model="addForm.connection"></el-input>
        </el-form-item>
        <el-form-item label="文件夹" prop="folder">
          <el-input v-model="addForm.folder"></el-input>
        </el-form-item>
        <!-- 读写标识 -->
        <el-form-item label="读写标识" prop="read_write_flag">
          <el-select v-model="addForm.read_write_flag" clearable placeholder="请选择">
            <el-option
              v-for="item in read_write_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSystemInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog
      title="修改"
      :visible.sync="updateDialogVisible"
      width="50%"
      @close="updateDialogClose"
    >
      <!-- 内容主体区域 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef" label-width="100px">
        <!-- 用户名 -->
        <el-form-item label="代码" prop="code">
          <el-input v-model="updateForm.code"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="updateForm.name"></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="IP" prop="ip">
          <el-input v-model="updateForm.ip"></el-input>
        </el-form-item>
        <el-form-item label="数据源类型" prop="db_type">
          <el-input v-model="updateForm.db_type"></el-input>
        </el-form-item>
        <el-form-item label="连接字符串" prop="connection">
          <el-input v-model="updateForm.connection"></el-input>
        </el-form-item>
        <el-form-item label="文件夹" prop="folder">
          <el-input v-model="updateForm.folder"></el-input>
        </el-form-item>
        <!-- 角色 -->
        <el-form-item label="读写标识" prop="read_write_flag"  >
          <el-select v-model="updateForm.read_write_flag"  clearable placeholder="请选择">
            <el-option
              v-for="item in read_write_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateSystemInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getSystemInfo();
  },
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
      },
      systemInfo: [],
      total: 0,
      addDialogVisible: false, //对话框显示状态
      //新增表单信息
      addForm: {
        code: "",
        name: "",
        ip: "",
        db_type: "",
        connection: "",
        folder: "",
        read_write_flag: "",
      },
      //修改表单信息
      updateForm: {},
      // 显示/隐藏修改用户栏
      updateDialogVisible: false,
      updateFormRules: {
        code: [
          { required: true, message: "请输入代码", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        ip: [
          { required: true, message: "请输入IP", trigger: "blur" },
          {
            min: 7,
            max: 15,
            message: "长度在 7 到 15 个字符",
            trigger: "blur",
          },
        ],
        db_type: [
          { required: true, message: "请输入数据源类型", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        connection: [
          { required: true, message: "请输入连接字符串", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        folder: [
          { required: true, message: "请输入文件夹", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        read_write_flag: [
          { required: true, message: "请输入读写标识", trigger: "blur" },
          {
            min: 1,
            max: 1,
            message: "长度在 1 到 1 个字符",
            trigger: "blur",
          },
        ],
      },
      addFormRules: {
        code: [
          { required: true, message: "请输入代码", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        ip: [
          { required: true, message: "请输入IP", trigger: "blur" },
          {
            min: 7,
            max: 15,
            message: "长度在 7 到 15 个字符",
            trigger: "blur",
          },
        ],
        db_type: [
          { required: true, message: "请输入数据源类型", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        connection: [
          { required: true, message: "请输入连接字符串", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        folder: [
          { required: true, message: "请输入文件夹", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        read_write_flag: [
          { required: true, message: "请输入读写标识", trigger: "blur" },
          {
            min: 1,
            max: 1,
            message: "长度在 1 到 1 个字符",
            trigger: "blur",
          },
        ],
      },
      read_write_options: [
        {
          value: "1",
          label: "Read",
        },
        {
          value: "2",
          label: "Push",
        },
      ],
      value: "",
    };
  },
  methods: {
    async getSystemInfo() {
      const { data: res } = await this.$http.get("systeminfo", {
        params: this.queryInfo,
      });
      this.systemInfo = res.data;
      this.total = res.num;
    },
    // 最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getSystemInfo();
    },
    // pageNum触发动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getSystemInfo();
    },
    // 监听添加用户操作
    addDialogClose() {
      this.$refs.addFormRef.resetFields();
    },
    // 添加用户
    addSystemInfo() {
      this.$refs.addFormRef.validate(async (valid) => {
        console.log(this.addForm);
        if (!valid) return;
        // 发起请求
        const { data: res } = await this.$http.post(
          "addsysteminfo",
          this.addForm
        );
        console.log(res);
        if (res != "Success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getSystemInfo();
      });
    },
    async deleteSystemInfo(id) {
      const confirmResult = await this.$confirm("确认是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.post("deletesysteminfo?id=" + id);
      if (res != "Success") {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getSystemInfo();
    },
    // 显示对话框
    async showUpdateDialog(id) {
      const { data: res } = await this.$http.get(
        "getupdatesysteminfo?id=" + id
      );
      console.log("res=" + res);
      this.updateForm = res;
      this.updateDialogVisible = true;
    },
    // 关闭对话框
    updateDialogClose() {
      // 重置信息
      this.$refs.updateFormRef.resetFields();
    },
    // 确认修改
    updateSystemInfo() {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改请求
        const { data: res } = await this.$http.post(
          "updatesysteminfo",
          this.updateForm
        );
        console.log(res);
        if (res != "Success") return this.$message.err("修改失败");
        this.$message.success("修改成功");
        this.updateDialogVisible = false;
        this.getSystemInfo();
      });
    },
  flagFormat(row, column) {
    if (row.read_write_flag == 1) {
      return 'Read'
    } else  {
      return 'Push'
    } 
  },

  },
};
</script>
<style lang="less">
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>